package Controllers;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;

import Bean.BeanBook;
import Bean.BeanOrder;
import Impl.BookServiceImpl;
import Impl.OrderServiceImpl;
import exception.BooksNotFoundException;

/**
 * Servlet implementation class RecommendController
 */
@Controller
public class RecommendController extends HttpServlet {
	BookServiceImpl bookService;

	@RequestMapping("/Recommend")
	protected void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		System.out.println("11111111111");
//		response.setContentType("text/html;charset=UTF-8");
//		JSONArray arr = new JSONArray();
//		ApplicationContext applicationContext = (ApplicationContext) request.getSession().getServletContext()
//				.getAttribute("ctx");
//		bookService = (BookServiceImpl) applicationContext.getBean("BookServiceImpl");
//		Collection<BeanBook> orders;
//		String userid = (String) request.getSession().getAttribute("userid");
		try {
			
//			if (!userid.isEmpty()) {
//				Socket socket = new Socket("192.168.43.230", 8001);
//				InputStream is = socket.getInputStream();
//				System.out.println("success");
//				ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(
//						userid.getBytes(Charset.forName("UTF-8")));
//				BufferedReader br = new BufferedReader(new InputStreamReader(byteArrayInputStream2));
//				PrintWriter write = new PrintWriter(socket.getOutputStream());
//				String readline;
//				readline = br.readLine();
//				write.println(readline);
//				write.flush();
//				System.out.println("Client:" + readline);
//				readline = br.readLine();
//				BufferedReader br2 = new BufferedReader(new InputStreamReader(is));
//				String mess = br2.readLine();
//				List<String> bookids = Arrays.asList(mess.split(";"));
//				System.out.println("message:" + mess);
//				for (int i = 0; i < bookids.size(); i++) {
//					BeanBook book = bookService.getBookDetails(bookids.get(i));
//					arr.add(book.toJson());
//				}
//				write.close();
//				is.close();
//				socket.close();
//			}
		} catch (Exception e) {
			System.out.println("can not listen to:" + e);
		}
//		response.getWriter().append(arr.toJSONString());
	}
}
